package service

import (
	emodel "engine/model"
	"gorm.io/gorm"
	"lib/log"
	"math"
	"time"
)

func CreateUserLogAll(tx *gorm.DB, stype int, user emodel.My_user, num float64, flag int, aid uint64, str string) error {

	myUserlog := emodel.My_user_score_log{
		Memo:   str,
		UserId: user.Id,
		Score:  num,
		//Before:     user.Money,
		//After:      afterMoney,
		Mobile:     user.Mobile,
		Aid:        aid,
		Flag:       flag,
		Createtime: time.Now().Format("2006-01-02T15:04:05"),
	}

	switch stype {
	case 0: //tz
		myUserlog.Before = user.Tz
		myUserlog.After = myUserlog.Before + num
		myUserlog.Type = 3
		tx.Exec("update my_user set tz=tz+? where id=? ", num, user.Id)
	case 1: //tz1
		myUserlog.Before = user.Tz1
		myUserlog.After = myUserlog.Before + num
		myUserlog.Type = 7
		tx.Exec("update my_user set tz1=tz1+? where id=? ", num, user.Id)
	case 2: //tz2
		myUserlog.Before = user.Tz2
		myUserlog.After = myUserlog.Before + num
		myUserlog.Type = 8
		tx.Exec("update my_user set tz2=tz2+? where id=? ", num, user.Id)
	case 3: //tz2
		myUserlog.Before = user.Tz3
		myUserlog.After = myUserlog.Before + num
		myUserlog.Type = 9
		tx.Exec("update my_user set tz3=tz3+? where id=? ", num, user.Id)
	case 4: //tz2
		myUserlog.Before = user.Tz4
		myUserlog.After = myUserlog.Before + num
		myUserlog.Type = 10
		tx.Exec("update my_user set tz4=tz4+? where id=? ", num, user.Id)

	}

	EsCreateUserLogData(myUserlog)
	return nil
}

func CreateUserLog(tx *gorm.DB, user emodel.My_user, num float64, flag int, aid uint64, str string) error {
	afterMoney := user.Money + num
	//0是赠送 1是接受 2兑换矿机 3矿机收益  4认证奖励 5下级矿机收益 6转入质押 7取消质押 8  9系统充值
	//11通用转贡献 12 贡献转通用 13参与爬楼扣除 14参与爬楼获得  15发布购买 16接单 17收到金额 18取消订单 20投放广告 31购买商品 41阶梯订单惩罚 42兑换超级激活码 43游戏消耗
	//44兑换消费积分 45积分抽奖
	if flag == 0 || flag == 2 || flag == 6 || flag == 8 || flag == 11 || flag == 13 || flag == 15 || flag == 16 || flag == 20 ||
		flag == 31 || flag == 41 || flag == 42 || flag == 43 || flag == 44 || flag == 45 {
		afterMoney = user.Money - num
		num = 0 - num
	}
	myUserlog := emodel.My_user_score_log{
		Memo:       str,
		UserId:     user.Id,
		Score:      num,
		Before:     user.Money,
		After:      afterMoney,
		Mobile:     user.Mobile,
		Aid:        aid,
		Flag:       flag,
		Createtime: time.Now().Format("2006-01-02T15:04:05"),
	}

	if num > 0 {
		tx.Exec("update my_user set money=money+? where id=? ", math.Abs(num), user.Id)
	} else {
		tx.Exec("update my_user set money=money-? where id=? ", math.Abs(num), user.Id)
	}
	EsCreateUserLogData(myUserlog)
	return nil
}

func CreateUserScoreLog(tx *gorm.DB, user emodel.My_user, num float64, flag int, aid uint64, str string) error {

	afterMoney := user.Score + num
	//0是赠送 1是接受 2兑换矿机 3矿机收益  4认证奖励 5下级矿机收益 6转入质押 7取消质押 8  9系统充值 11通用转贡献 12 贡献转备用 13参与爬楼扣除 14参与爬楼获得 15发布购买 16接单 17收到金额 30订单退款
	if flag == 0 || flag == 2 || flag == 13 || flag == 16 || flag == 30 {
		afterMoney = user.Score - num
		num = 0 - num
	}

	myUserlog := emodel.My_user_score_log{
		Memo:       str,
		UserId:     user.Id,
		Score:      num,
		Before:     user.Score,
		After:      afterMoney,
		Mobile:     user.Mobile,
		Aid:        aid,
		Flag:       flag,
		Type:       1,
		Createtime: time.Now().Format("2006-01-02T15:04:05"),
	}

	if num > 0 {
		tx.Exec("update my_user set score=score+? where id=? ", math.Abs(num), user.Id)
	} else {
		tx.Exec("update my_user set score=score-? where id=? ", math.Abs(num), user.Id)
	}
	EsCreateUserLogData(myUserlog)
	return nil
}

func CreateUserPointLog(tx *gorm.DB, user emodel.My_user, num float64, flag int, aid uint64, str string) error {

	afterMoney := user.Point + num
	//0是赠送 1是接受 2兑换矿机 3矿机收益  4认证奖励 5下级矿机收益 6转入质押 7取消质押 8  9系统充值 11通用转贡献
	//12 贡献转通用 13参与爬楼扣除 14参与爬楼获得 15激活 19接替马 20悬赏 31贡献基本投放 32接单扣除贡献积分 33放行 34取消接单 35购物返利 36游戏消耗积分
	//37兑换消费积分 38购买商品 39积分抽盲盒
	if flag == 0 || flag == 12 || flag == 31 || flag == 32 || flag == 36 || flag == 37 || flag == 38 || flag == 39 {
		afterMoney = user.Point - num
		num = 0 - num
	}

	myUserlog := emodel.My_user_score_log{
		Memo:       str,
		UserId:     user.Id,
		Score:      num,
		Before:     user.Point,
		After:      afterMoney,
		Mobile:     user.Mobile,
		Aid:        aid,
		Flag:       flag,
		Type:       2,
		Createtime: time.Now().Format("2006-01-02T15:04:05"),
	}

	if num > 0 {
		tx.Exec("update my_user set point=point+? where id=? ", math.Abs(num), user.Id)
	} else {
		tx.Exec("update my_user set point=point-? where id=? ", math.Abs(num), user.Id)
	}

	EsCreateUserLogData(myUserlog)
	return nil
}

func CreateTzLog(tx *gorm.DB, user emodel.My_user, num float64, flag int, aid uint64, str string) error {
	afterMoney := user.Tz + num
	//flag 0兑换 1转赠获得 2消费积分 3提取 4提取驳回 10系统操作 36游戏的奖励 37通天塔奖励 38抽奖1 39抽奖40
	//50转赠 51养鱼分红 52直推捞鱼 53间推捞鱼 54参加游戏2 55游戏2的奖励 56后台取消转赠 57购买桐铁楸 58购买银铁楸 59购买金铁楸
	if flag == 3 || flag == 38 || flag == 50 || flag == 54 || flag == 57 || flag == 58 || flag == 59 {
		afterMoney = user.Tz - num
		num = 0 - num
	}
	myUserlog := emodel.My_user_score_log{
		Memo:       str,
		UserId:     user.Id,
		Score:      num,
		Before:     user.Tz,
		After:      afterMoney,
		Mobile:     user.Mobile,
		Aid:        aid,
		Flag:       flag,
		Type:       3,
		Createtime: time.Now().Format("2006-01-02T15:04:05"),
	}

	if num > 0 {
		errs := tx.Exec("update my_user set tz=tz+? where id=? ", math.Abs(num), user.Id).Error
		if errs != nil {
			log.Debugf("1 CreateTzLog(tx *tr st err %v  num%d  user.Id%d", errs, num, user.Id)
			return errs
		}
	} else {
		errs := tx.Exec("update my_user set tz=tz-? where id=? ", math.Abs(num), user.Id).Error
		if errs != nil {
			log.Debugf("2 CreateTzLog(tx *tr st err %v  num%d  user.Id%d", errs, num, user.Id)
			return errs
		}
	}
	EsCreateUserLogData(myUserlog)
	return nil
}

func CreateGoldLog(tx *gorm.DB, user emodel.My_user, num float64, flag int, aid uint64, str string) error {
	afterMoney := user.Gold + num
	//flag 0兑换 1转赠获得 2消费积分
	if flag == 2 {
		afterMoney = user.Gold - num
		num = 0 - num
	}
	myUserlog := emodel.My_user_score_log{
		Memo:       str,
		UserId:     user.Id,
		Score:      num,
		Before:     user.Gold,
		After:      afterMoney,
		Mobile:     user.Mobile,
		Aid:        aid,
		Flag:       flag,
		Type:       4,
		Createtime: time.Now().Format("2006-01-02T15:04:05"),
	}

	if num > 0 {
		tx.Exec("update my_user set gold=gold+? where id=? ", math.Abs(num), user.Id)
	} else {
		tx.Exec("update my_user set gold=gold-? where id=? ", math.Abs(num), user.Id)
	}
	EsCreateUserLogData(myUserlog)
	return nil
}

func CreateSlLog(tx *gorm.DB, user emodel.My_user, num float64, flag int, aid uint64, str string) error {
	afterMoney := user.Sl + num
	//flag 1.自动兑换饲料  2.喂养鱼 3完成任务 4余额生息 5每局杀手1的分红 6直推喂养 7间推喂养 8杀手儿投注 9参与饲料抢地主获得
	//10参与饲料 11用户分红 12 兑换矿石
	if flag == 2 || flag == 10 {
		afterMoney = user.Sl - num
		num = 0 - num
	}
	myUserlog := emodel.My_user_score_log{
		Memo:       str,
		UserId:     user.Id,
		Score:      num,
		Before:     user.Sl,
		After:      afterMoney,
		Mobile:     user.Mobile,
		Aid:        aid,
		Flag:       flag,
		Type:       11,
		Createtime: time.Now().Format("2006-01-02T15:04:05"),
	}

	if num > 0 {
		tx.Exec("update my_user set sl=sl+? where id=? ", math.Abs(num), user.Id)
	} else {
		tx.Exec("update my_user set sl=sl-? where id=? ", math.Abs(num), user.Id)
	}
	EsCreateUserLogData(myUserlog)
	return nil
}
